package com.jdls.tanhua.server;

import com.jdls.tanhua.dubbo.CommentApi;
import com.jdls.tanhua.dubbo.MovementApi;
import com.jdls.tanhua.dubbo.UserInfoApi;
import com.jdls.tanhua.enums.CommentType;
import com.jdls.tanhua.interceptor.UserHolder;
import com.jdls.tanhua.mongo.Comment;
import com.jdls.tanhua.mongo.Movement;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.vo.CommentVo;
import com.jdls.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/10/29
 */
@Service
public class CommentServer {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;


    //发布评论
    public void saveComment(Map map) {

        Comment comment = new Comment();
        //获取动态id
        String mid = (String) map.get("movementId");
        ObjectId movementId = new ObjectId(mid);
        //设置动态id
        comment.setPublishId(movementId);
        String content = (String) map.get("comment");
        comment.setContent(content);

        //设置互动类型
        comment.setCommentType(CommentType.COMMENT.getType());
        //设置当前评论人的id
        comment.setUserId(UserHolder.getUserId());
        //设置当前时间
        comment.setCreated(System.currentTimeMillis());
        //获取发布当前动态人的id
        //Movement movement = movementApi.findOne(movementId);
        //设置当前动态发布人的id
        //comment.setPublishUserId(movement.getUserId());

        Integer save = commentApi.save(comment);
    }

    public PageResult findComment(String movementId, Integer page, Integer pagesize) {
        ObjectId mid = new ObjectId(movementId);
        //根据动态id获取当前动态下所有评论
        List<Comment> comments =  commentApi.findAll(mid,page,pagesize);
        if (comments == null||comments.size() == 0){
            return new PageResult();
        }
        //获取所有评论人的id
        List<Long> ids = new ArrayList<>();
        for (Comment comment : comments) {
            ids.add(comment.getUserId());
        }
        //根据所有评论人的id获取评论人的基本信息
        List<UserInfo> userInfos = userInfoApi.findAll(ids);

        //构造vo
        List<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : comments) {
            for (UserInfo userInfo : userInfos) {
                if (comment.getUserId() ==  userInfo.getId()){
                    CommentVo vo = CommentVo.init(userInfo, comment);
                    //主key
                    String key = comment.getId().toHexString();
                    //喜欢key
                    String likeKey = CommentType.LIKE+"_"+UserHolder.getUserId();
                    //判断当前用户是否已对这条评论点赞
                    if (redisTemplate.opsForHash().hasKey(key,likeKey)){
                        vo.setHasLiked(1);
                    }
                    commentVos.add(vo);
                    break;
                }
            }
        }
        PageResult pageResult = new PageResult(page,pagesize,commentVos.size(),commentVos);
        return pageResult;
    }

    public Integer like(String id) {
        Long userId = UserHolder.getUserId();
        ObjectId commentId = new ObjectId(id);
        //主key
        String key = id;
        //喜欢key
        String likeKey = CommentType.LIKE+"_"+userId;
        redisTemplate.opsForHash().put(key,likeKey,"1");

        Integer like = commentApi.like(commentId);
        return like;
    }

    public Integer dislike(String id) {
        Long userId = UserHolder.getUserId();
        ObjectId commentId = new ObjectId(id);
        //主key
        String key = id;
        //喜欢key
        String likeKey = CommentType.LIKE+"_"+userId;
        redisTemplate.opsForHash().delete(key,likeKey);
        Integer dislike = commentApi.dislike(commentId);
        return dislike;
    }
}
